using System.IO;
using System.Windows.Forms;
using Curriculum.DataSet;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using E4RPDevExpress;

namespace Curriculum.Controls
{
    public partial class CandidatesXtraUserControl : XtraUserControl, IGridView, ICandidateList
    {
        private BackOfficeController _controller;
        
        #region Properties

        public BackOfficeController Controller
        {
            set
            {
                _controller = value;
                //SetGridDataSource();
                _controller.CandidateSaved += delegate { SetGridDataSource(); };                
            }
        }

        #endregion

        public CandidatesXtraUserControl()
        {
            InitializeComponent();
            gridView.FocusedRowChanged += GridViewFocusedRowChanged;
            attachmentRepositoryItemButtonEdit.ButtonClick += AttachmentRepositoryItemButtonEditButtonClick;
            actionGridView.DoubleClick += ActionGridViewDoubleClick;
        }

        public void Reload()
        {
            SetGridDataSource();
        }

        #region Private Methods

        public string GetExtendedFilter()
        {
            if (ReferenceEquals(gridView.ActiveFilterCriteria, null))
                return "";
            return gridView.ActiveFilterCriteria.LegacyToString();
        }

        private void SetGridDataSource()
        {
            var focusedRowHandle = gridView.FocusedRowHandle;
            var dataSet = _controller.GetCandidates(GetExtendedFilter());
            gridControl.DataSource = null;
            gridControl.DataSource = dataSet;
            gridControl.DataMember = dataSet.Candidate.TableName;
            if (dataSet.Candidate.Count > 0)
                gridView.FocusedRowHandle = 0;
            if (focusedRowHandle >= 0)
                gridView.FocusedRowHandle = focusedRowHandle;
        }
        
        #endregion        

        #region IGridView

        public GridView GridView()
        {
            return gridView;
        }

        public string ListName
        {
            get { return "Candidates"; }
        }

        #endregion

        #region ICandidateList

        public long? CandidateId
        {
            get
            {
                var candidate = gridView.GetDataRow(gridView.FocusedRowHandle);
                if (candidate == null)
                    return null;                
                return ((CandidateDataSet.CandidateRow)candidate).Id;
            }
        }

        public long? VacancyId { get{ return null;} }

        #endregion

        void GridViewFocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            var candidate = (CandidateDataSet.CandidateRow)gridView.GetDataRow(gridView.FocusedRowHandle);
            actionGridControl.DataSource = null;
            if (candidate == null)
                return;
            var dataSet = _controller.GetCandidate(candidate.Id);
            actionGridControl.DataSource = null;
            actionGridControl.DataSource = dataSet;
            actionGridControl.DataMember = dataSet.CandidateAction.TableName;
        }

        void AttachmentRepositoryItemButtonEditButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var candidate = (CandidateDataSet.CandidateRow)gridView.GetDataRow(gridView.FocusedRowHandle);
            if (candidate.IsAttachmentTypeNull() || !candidate.HasAttachment)
                return;
            var tempFile = Path.GetTempPath() + "\\" + candidate.AttachmentName;
            var fileStream = new FileStream(tempFile, FileMode.Create);
            var attachment = _controller.GetCandidateAttachment(candidate.Id);
            fileStream.Write(attachment, 0, attachment.Length);
            fileStream.Close();
            System.Diagnostics.Process.Start(tempFile);
        }

        void ActionGridViewDoubleClick(object sender, System.EventArgs e)
        {
            var action = (CandidateDataSet.CandidateActionRow) actionGridView.GetDataRow(actionGridView.FocusedRowHandle);
            if(action == null)
                return;
            var form = new ChangeStateXtraForm();
            if (!action.IsReasonNull()) form.Reason = action.Reason;
            if (!action.IsInterviewerNull()) form.Interviewer = action.Interviewer;
            if (!action.IsRemarksNull()) form.Remarks = action.Remarks;
            if (!action.IsDateNull()) form.Date = action.Date;
            if (form.ShowDialog() != DialogResult.OK)
                return;
            _controller.UpdateCandidateAction(action.ActionId, form.Reason, form.Remarks, form.Date, form.Interviewer);
        }


    }
}